﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using FrbaBus.Properties;

namespace FrbaBus.AbmMicro
{
    public partial class FormButacas : Form
    {
        private long micro; //codigo de micro
        private int butacas; //cantidad de butacas

        public FormButacas(long codigoMicro, int cantButacas)
        {

            this.micro = codigoMicro;
            this.butacas = cantButacas;

            //ALTA
            InitializeComponent();



            grillaButacas.Columns.Add("but_codigo", "ASIENTO");
            grillaButacas.Columns.Add("but_tipo", "TIPO (P: Pasillo/V:Ventanilla)");
            grillaButacas.Columns.Add("but_piso", "PISO");

            grillaButacas.Rows.Add(cantButacas - 1);

        }

        public FormButacas(long codigoMicro)
        {

            this.micro = codigoMicro;

            //MODIFICACION
            InitializeComponent();

            grillaButacas.Columns.Add("but_codigo", "ASIENTO");
            grillaButacas.Columns.Add("but_tipo", "TIPO (P: Pasillo/V:Ventanilla)");
            grillaButacas.Columns.Add("but_piso", "PISO");


            Cargar_Grilla(codigoMicro);
        }

        private void Cargar_Grilla(long codigoMicro)
        {
            grillaButacas.Rows.Clear();

            string query;
            string tipoButaca = "";

            query = " SELECT but_codigo, but_tipo, but_piso";
            query += " FROM THE_BEST_IAS.butacas BU ";
            query += " WHERE BU.mic_codigo = " + Convert.ToInt64(codigoMicro); 

            query += " ORDER BY 1";

            SqlDataReader dr = DbHelper.ExecuteQuery(query);

            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    if (dr["but_tipo"].ToString().Trim() == "Pasillo")
                    {
                        tipoButaca = "P";
                    }
                    else {
                        tipoButaca = "V";
                    
                    }

                    grillaButacas.Rows.Add(new object[] { 
                           dr["but_codigo"].ToString().Trim(), 
                           tipoButaca, 
                           dr["but_piso"].ToString().Trim(), 
   
                         });
                }
            }

        }

        private void FormButacas_Load(object sender, EventArgs e)
        {

        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {

            int butaca = 0;
            string tipo = "";
            int piso = 0;

            for(int i =0 ; i < grillaButacas.RowCount - 1 ; i++){

                if (grillaButacas.Rows[i].Cells[0].Value != null)
                {
                    if (grillaButacas.Rows[i].Cells[0].Value.ToString() != String.Empty)
                    {
                        butaca = Convert.ToInt32(grillaButacas.Rows[i].Cells[0].Value.ToString());
                    }
                    else
                    {
                        MessageBox.Show("Por favor, revisar los valores ingresados", "ERROR");
                        return;
                    }
                }

                if (grillaButacas.Rows[i].Cells[1].Value != null)
                {
                    if (grillaButacas.Rows[i].Cells[1].Value.ToString() == "P")
                    {
                        tipo = "Pasillo";
                    }
                    if (grillaButacas.Rows[i].Cells[1].Value.ToString() == "V")
                    {
                        tipo = "Ventanilla";
                    }
                    if (grillaButacas.Rows[i].Cells[1].Value.ToString() != "P" && grillaButacas.Rows[i].Cells[1].Value.ToString() != "V")
                    {
                        MessageBox.Show("Por favor, revisar los valores ingresados", "ERROR");
                        return;

                    }
                }

                if (grillaButacas.Rows[i].Cells[2].Value != null)
                {
                    if (grillaButacas.Rows[i].Cells[2].Value.ToString() != String.Empty)
                    {
                        piso = Convert.ToInt32(grillaButacas.Rows[i].Cells[2].Value.ToString());
                    }
                    else
                    {
                        MessageBox.Show("Por favor, revisar los valores ingresados", "ERROR");
                        return;
                    }
                }

                if (grillaButacas.Rows[i].Cells[0].Value != null & grillaButacas.Rows[i].Cells[1].Value != null & grillaButacas.Rows[i].Cells[2].Value != null)
                {

                    string sConnectionString = Settings.Default.GD1C2013ConnectionString;
                    SqlConnection connection = new SqlConnection(sConnectionString);
                    SqlCommand cmd = new SqlCommand("THE_BEST_IAS.pr_alta_butaca", connection);
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.Add(new SqlParameter("@p_mic_codigo", SqlDbType.BigInt)).Value = this.micro;
                    cmd.Parameters.Add(new SqlParameter("@p_but_codigo", SqlDbType.BigInt)).Value = butaca;
                    cmd.Parameters.Add(new SqlParameter("@p_but_piso", SqlDbType.BigInt)).Value = piso;
                    cmd.Parameters.Add(new SqlParameter("@but_tipo", SqlDbType.VarChar)).Value = tipo;

                    //cmd.Parameters.Add("@returnValue", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
                    cmd.CommandTimeout = 1000;

                    connection.Open();
                    cmd.ExecuteNonQuery();

                    //int valorRetorno = Convert.ToInt32(cmd.Parameters["@returnValue"].Value);

                    connection.Close();

                    //if (valorRetorno == 0)
                    //{

                    //}
                }
            
            }
            MessageBox.Show("El micro " + this.micro + " ha sido registrado de manera exitosa");
            this.Hide();

        }
    }
}
